<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="../compil/index.html">&lt; 0. Compiling with libtcod</a>
</td><td width="60%">
<p align="center">
=====================================<br />
1. Colors<br />
=====================================<br />
</p></td><td width="20%">
<a href="../console/index.html">&gt; 2. Console emulator</a>
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
table.color td { width:60px; text-align: center;}
</style>
<a href="#1.1">1.1 Predefined colors<br /></a>
<a href="#1.2">1.2 User defined colors<br /></a>
<a href="#1.3">1.3 Comparing colors<br /></a>
<a href="#1.4">1.4 Colors operators<br /></a>
<a href="#1.5">1.5 Interpolating colors<br /></a>
<a href="#1.6">1.6 HSV conversions<br /></a>
<a href="#1.7">1.7 Color map generation<br /></a>
<br />
<hr />
<a name="1.1"></a>
Note that the Doryen library uses 32bits colors. Thus, your OS desktop must use 32bits colors.<br />
A color is defined by its red, green and blue component between 0 and 255.<br />You can use predefined colors :<br />
<table class='color'>
<tr><td style="text-align:right">color</td><td>red</td><td>orange</td><td>yellow</td><td>chartreuse</td><td>green</td><td>sea</td><td>cyan</td><td>sky</td><td>blue</td><td>violet</td><td>magenta</td><td>pink</td></tr>
<tr><td style="text-aligne:left">level</td><td colspan="12"></td></tr>
<tr><td>desaturated</td><td style="background-color:rgb(127,63,63)"></td><td style="background-color:rgb(127,95,63)"></td><td style="background-color:rgb(127,127,63)"></td><td style="background-color:rgb(95,127,63)"></td><td style="background-color:rgb(63,127,63)"></td><td style="background-color:rgb(63,127,95)"></td><td style="background-color:rgb(63,127,127)"></td><td style="background-color:rgb(63,95,127)"></td><td style="background-color:rgb(63,63,127)"></td><td style="background-color:rgb(95,63,127)"></td><td style="background-color:rgb(127,63,127)"></td><td style="background-color:rgb(127,63,95)"></td></tr>
<tr><td>light</td><td style="background-color:rgb(255,127,127)"></td><td style="background-color:rgb(255,191,127)"></td><td style="background-color:rgb(255,255,127)"></td><td style="background-color:rgb(191,255,127)"></td><td style="background-color:rgb(127,255,127)"></td><td style="background-color:rgb(127,255,191)"></td><td style="background-color:rgb(127,255,255)"></td><td style="background-color:rgb(127,191,255)"></td><td style="background-color:rgb(127,127,255)"></td><td style="background-color:rgb(191,127,255)"></td><td style="background-color:rgb(255,127,255)"></td><td style="background-color:rgb(255,127,191)"></td></tr>
<tr><td>normal</td><td style="background-color:rgb(255,0,0)"></td><td style="background-color:rgb(255,127,0)"></td><td style="background-color:rgb(255,255,0)"></td><td style="background-color:rgb(127,255,0)"></td><td style="background-color:rgb(0,255,0)"></td><td style="background-color:rgb(0,255,127)"></td><td style="background-color:rgb(0,255,255)"></td><td style="background-color:rgb(0,127,255)"></td><td style="background-color:rgb(0,0,255)"></td><td style="background-color:rgb(127,0,255)"></td><td style="background-color:rgb(255,0,255)"></td><td style="background-color:rgb(255,0,127)"></td></tr>
<tr><td>dark</td><td style="background-color:rgb(127,0,0)"></td><td style="background-color:rgb(127,63,0)"></td><td style="background-color:rgb(127,127,0)"></td><td style="background-color:rgb(63,127,0)"></td><td style="background-color:rgb(0,127,0)"></td><td style="background-color:rgb(0,127,63)"></td><td style="background-color:rgb(0,127,127)"></td><td style="background-color:rgb(0,63,127)"></td><td style="background-color:rgb(0,0,127)"></td><td style="background-color:rgb(63,0,127)"></td><td style="background-color:rgb(127,0,127)"></td><td style="background-color:rgb(127,0,63)"></td></tr>
<tr><td>darker</td><td style="background-color:rgb(63,0,0)"></td><td style="background-color:rgb(63,31,0)"></td><td style="background-color:rgb(63,63,0)"></td><td style="background-color:rgb(31,63,0)"></td><td style="background-color:rgb(0,63,0)"></td><td style="background-color:rgb(0,63,31)"></td><td style="background-color:rgb(0,63,63)"></td><td style="background-color:rgb(0,31,63)"></td><td style="background-color:rgb(0,0,63)"></td><td style="background-color:rgb(31,0,63)"></td><td style="background-color:rgb(63,0,63)"></td><td style="background-color:rgb(63,0,31)"></td></tr>
</table>
Or grey levels :
<table class='color'>
<tr><td></td><td>black</td><td>darker grey</td><td>dark grey</td><td>grey</td><td>light grey</td><td>white</td></tr>
<tr><td>&nbsp;</td><td style="background-color:rgb(0,0,0)"></td><td style="background-color:rgb(31,31,31)"></td><td style="background-color:rgb(63,63,63)"></td><td style="background-color:rgb(128,128,128)"></td><td style="background-color:rgb(191,191,191)"></td><td style="background-color:rgb(255,255,255)"></td></tr>
</table>
<p class="code"><pre>
C++ : TCODColor::desaturatedRed
      TCODColor::lightRed
      TCODColor::red
      TCODColor::darkRed
      TCODColor::darkerRed
C   : TCOD_desaturated_red
      TCOD_light_red
      TCOD_red
      TCOD_dark_red
      TCOD_darker_red
Py  : libtcod.desaturated_red
      libtcod.light_red
      libtcod.red
      libtcod.dark_red
      libtcod.darker_red
</pre></p>

<a name="1.2"></a>
or define your own :<br />
<p class="code"><pre>
C++ : TCODColor myColor(24,64,255);
C   : TCOD_color_t my_color={24,64,255};
Py  : my_color=libtcod.Color(24,64,255)
</pre></p>
<hr />
<a name="1.3"></a>
You can compare two colors :<br />
<p class="code"><pre>
C++ :
  if (myColor == TCODColor::yellow ) { ... }<br />
  if (myColor != TCODColor::white ) { ... }
C :
  if ( TCOD_color_equals(my_color, TCOD_yellow ) ) { ... }<br />
  if ( ! TCOD_color_equals(my_color, TCOD_white ) ) { ... }
Py :
  if my_color ==  libtcod.yellow : ... <br />
  if my_color != litbcod.white : ... 
</pre></p>
<br />
<hr />
<a name="1.4"></a>
You can multiply two colors :<br />
<table>
	<tr><td class="code"><pre>C++ : TCODColor myDarkRed = </pre></td><td class="code"><pre>TCODColor::darkGrey *</pre></td><td class="code"><pre>TCODColor::lightRed;</pre></td></tr>
	<tr><td class="code"><pre>C   : TCOD_color_t my_dark_red = TCOD_color_multiply(</pre></td><td class="code"><pre>TCOD_dark_grey,</pre></td><td class="code"><pre>TCOD_light_red);</pre></td></tr>
	<tr><td class="code"><pre>Py  : my_dark_red =</pre></td><td class="code"><pre>libtcod.dark_grey *</pre></td><td class="code"><pre>libtcod.light_red</pre></td></tr>
	<tr><td style="background-color:rgb(96,0,0);width:60px;height:30px"></td><td style="background-color:rgb(96,96,96);width:60px"></td><td style="background-color:rgb(255,0,0);width:60px"></td></tr>
</table>
<p class="code"><pre>
  c1 = c2 * c3 => c1.r = c2.r * c3.r /255<br />
                  c1.g = c2.g * c3.g /255<br />
                  c1.b = c2.b * c3.b /255
</pre></p>                  
or multiply a color by a float :<br />
<table>
	<tr><td class="code"><pre>C++ : TCODColor myDarkRed = </pre></td><td class="code"><pre>TCODColor::lightRed *</pre></td><td class="code"><pre>0.5f;</pre></td></tr>
	<tr><td class="code"><pre>C   : TCOD_color_t my_dark_red = TCOD_color_multiply_scalar(</pre></td><td class="code"><pre>TCOD_light_red,</pre></td><td class="code"><pre>0.5f);</pre></td></tr>
	<tr><td class="code"><pre>Py  : myDarkRed = </pre></td><td class="code"><pre>litbcod.light_red *</pre></td><td class="code"><pre>0.5</pre></td></tr>
	<tr><td style="background-color:rgb(128,0,0);width:60px;height:30px"></td><td style="background-color:rgb(255,0,0);width:60px"></td><td style="width:60px"></td></tr>
</table>
<p class="code"><pre>
  c1 = c2 * v => c1.r = CLAMP(0, 255, c2.r * v)<br />
                  c1.g = CLAMP(0, 255, c2.g * v)<br />
                  c1.b = CLAMP(0, 255, c2.b * v)
</pre></p>                  
or add two colors :<br />
<table>
	<tr><td class="code"><pre>C++ : TCODColor myRed = </pre></td><td class="code"><pre>TCODColor::lightRed +</pre></td><td class="code"><pre>TCODColor::darkGrey</pre></td></tr>
	<tr><td class="code"><pre>C   : TCOD_color_t my_red = TCOD_color_add(</pre></td><td class="code"><pre>TCOD_light_red,</pre></td><td class="code"><pre>TCOD_dark_grey);</pre></td></tr>
	<tr><td class="code"><pre>Py  : myRed = </pre></td><td class="code"><pre>libtcod.light_red +</pre></td><td class="code"><pre>libtcod.dark_grey</pre></td></tr>
	<tr><td style="background-color:rgb(255,128,128);width:60px;height:30px"></td><td style="background-color:rgb(255,0,0);width:60px"></td><td style="background-color:rgb(128,128,128);width:60px"></td></tr>
</table>
<p class="code"><pre>
  c1 = c1 + c2 => c1.r = MIN(255, c1.r + c2.r)<br />
                  c1.g = MIN(255, c1.g + c2.g)<br />
                  c1.b = MIN(255, c1.b + c2.b)
</pre></p>                  
or subtract two colors :<br />
<table>
	<tr><td class="code"><pre>C++ : TCODColor myRed = </pre></td><td class="code"><pre>TCODColor::lightRed -</pre></td><td class="code"><pre>TCODColor::darkGrey</pre></td></tr>
	<tr><td class="code"><pre>C   : TCOD_color_t my_red = TCOD_color_subtract(</pre></td><td class="code"><pre>TCOD_light_red,</pre></td><td class="code"><pre>TCOD_dark_grey);</pre></td></tr>
	<tr><td class="code"><pre>Py  : myRed = </pre></td><td class="code"><pre>libtcod.light_red -</pre></td><td class="code"><pre>libtcod.dark_grey</pre></td></tr>
	<tr><td style="background-color:rgb(127,0,0);width:60px;height:30px"></td><td style="background-color:rgb(255,0,0);width:60px"></td><td style="background-color:rgb(128,128,128);width:60px"></td></tr>
</table>
<p class="code"><pre>
  c1 = c1 - c2 => c1.r = MAX(0, c1.r - c2.r)<br />
                  c1.g = MAX(0, c1.g - c2.g)<br />
                  c1.b = MAX(0, c1.b - c2.b)
</pre></p>                  
<hr />
<a name="1.5"></a>
You can interpolate between two colors with<br />
<p class="code"><pre>C++ : static TCODColor TCODColor::lerp(TCODColor a, TCODColor b, float coef)</pre></p>
<p class="code"><pre>C   : TCOD_color_t TCOD_color_lerp(TCOD_color_t a, TCOD_color_t b, float coef)</pre></p>
<p class="code"><pre>Py  : color_lerp(a, b, coef)</pre></p>
<table>
	<tr><td class="code"><pre>C++ : TCODColor myColor = TCODColor::lerp ( </pre></td><td class="code"><pre>TCODColor::darkGrey,</pre></td><td class="code"><pre>TCODColor::lightRed,coef);</pre></td></tr>
	<tr><td class="code"><pre>C   : TCOD_color_t my_color = TCOD_color_lerp ( </pre></td><td class="code"><pre>TCOD_dark_grey,</pre></td><td class="code"><pre>TCOD_light_red,coef);</pre></td></tr>
	<tr><td class="code"><pre>Py  : my_color = libtcod.color_lerp ( </pre></td><td class="code"><pre>libtcod.dark_grey,</pre></td><td class="code"><pre>litbcod.light_red,coef)</pre></td></tr>
	<tr><td align="center" style="background-color:rgb(96,96,96);color:#ffffff;">coef == 0.0f</td><td style="background-color:rgb(96,96,96);width:60px"></td><td style="background-color:rgb(255,0,0);width:60px"></td></tr>
	<tr><td align="center" style="background-color:rgb(135,72,72);color:#ffffff;">coef == 0.25f</td><td style="background-color:rgb(96,96,96);width:60px"></td><td style="background-color:rgb(255,0,0);width:60px"></td></tr>
	<tr><td align="center" style="background-color:rgb(175,48,48);color:#ffffff;">coef == 0.5f</td><td style="background-color:rgb(96,96,96);width:60px"></td><td style="background-color:rgb(255,0,0);width:60px"></td></tr>
	<tr><td align="center" style="background-color:rgb(215,24,24);color:#ffffff;">coef == 0.75f</td><td style="background-color:rgb(96,96,96);width:60px"></td><td style="background-color:rgb(255,0,0);width:60px"></td></tr>
	<tr><td align="center" style="background-color:rgb(255,0,0);color:#ffffff;">coef == 1.0f</td><td style="background-color:rgb(96,96,96);width:60px"></td><td style="background-color:rgb(255,0,0);width:60px"></td></tr>
</table>
<p class="code"><pre>
  c1 = lerp (c2, c3, coef) => c1.r = c2.r  + (c3.r - c2.r ) * coef<br />
                              c1.g = c2.g  + (c3.g - c2.g ) * coef<br />
                              c1.b = c2.b  + (c3.b - c2.b ) * coef
</pre></p>  
  coef should be between 0.0 and 1.0 but you can as well use other values<br />
<br />
<hr />
<a name="1.6"></a>
You can define a color from its hue, saturation, value components :<br />
<p class="code"><pre>
C++ : void TCODColor::setHSV(float h, float s, float v)
C   : void TCOD_color_set_HSV(TCOD_color_t *c,float h, float s, float v)
Py  : color_set_HSV(c,h,s,v)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>c</td><td>In the C version, the TCOD_color_t to modify.</td></tr>
<tr><td>h,s,v</td><td>Color components in the HSV space<br />0.0 &lt;= h &lt; 360.0<br />0.0 &lt;= s &lt;= 1.0<br />0.0 &lt;= v &lt;= 1.0</td></tr>
</table>
After this function is called, the r,g,b fields of the color contains the red, green, blue values corresponding to the h,s,v parameters.<br />
<br />
You can get the HSV components of a color :<br />
<p class="code"><pre>
C++ : void TCODColor::getHSV(float *h, float *s, float *v) const
C   : void TCOD_color_get_HSV(TCOD_color_t c,float * h, float * s, float * v)
Py  : color_get_HSV(c) # returns [h,s,v]
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>c</td><td>In the C version, the TCOD_color_t from which to read.</td></tr>
<tr><td>h,s,v</td><td>Color components in the HSV space<br />0.0 &lt;= h &lt; 360.0<br />0.0 &lt;= s &lt;= 1.0<br />0.0 &lt;= v &lt;= 1.0</td></tr>
</table>
<br />
<hr />
<a name="1.7"></a>
You can define a color map from an array of color keys. Colors will be interpolated between the keys.<br />
<p class="code"><pre>
C++ : static void genMap(TCODColor *map, int nbKey, TCODColor const *keyColor, int const *keyIndex)
C   : void TCOD_gen_map(TCOD_color_t *map, int nb_key, TCOD_color_t const *key_color, int const *key_index)
Py  : gen_map(keyColor,keyIndex) # returns an array of colors
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>map</td><td>An array of colors to be filled by the function.</td></tr>
<tr><td>nbKey</td><td>Number of color keys</td></tr>
<tr><td>keyColor</td><td>Array of nbKey colors containing the color of each key</td></tr>
<tr><td>keyIndex</td><td>Array of nbKey integers containing the index of each key.<br />If you want to fill the map array, keyIndex[0] must be 0 and keyIndex[nbKey-1] is the number of elements in map minus 1 but you can also use the function to fill only a part of the map array.<br /></td></tr>
</table>
<br />
Example :
<p class="code"><pre>
C++ :
int idx[] = { 0, 4, 8 }; /* indexes of the keys */
TCODColor col[] = { TCODColor( 0,0,0 ), TCODColor(255,0,0), TCODColor(255,255,255) }; // colors : black, red, white
TCODColor map[9];
TCODColor::genMap(map,3,col,idx);
C   :
int idx[] = { 0, 4, 8 }; /* indexes of the keys */
TCOD_color_t col[] = { { 0,0,0 }, {255,0,0}, {255,255,255} }; /* colors : black, red, white */
TCOD_color_t map[9];
TCOD_color_gen_map(map,3,col,idx);
Py  :
idx = [ 0, 4, 8 ] # indexes of the keys 
col = [ libtcod.Color( 0,0,0 ), libtcod.Color( 255,0,0 ), libtcod.Color(255,255,255) ] # colors : black, red, white 
map=libtcod.color_gen_map(col,idx)
</pre></p>
Result :<br />
<table>
	<tr><td class="code"><pre>map[0]</pre></td><td align="center" style="background-color:rgb(0,0,0);color:#ffffff;width:50px">&nbsp;</td><td>black</td></tr>
	<tr><td class="code"><pre>map[1]</pre></td><td align="center" style="background-color:rgb(63,0,0);color:#ffffff;width:50px">&nbsp;</td></tr>
	<tr><td class="code"><pre>map[2]</pre></td><td align="center" style="background-color:rgb(127,0,0);color:#ffffff;width:50px">&nbsp;</td></tr>
	<tr><td class="code"><pre>map[3]</pre></td><td align="center" style="background-color:rgb(191,0,0);color:#ffffff;width:50px">&nbsp;</td></tr>
	<tr><td class="code"><pre>map[4]</pre></td><td align="center" style="background-color:rgb(255,0,0);color:#ffffff;width:50px">&nbsp;</td><td>red</td></tr>
	<tr><td class="code"><pre>map[5]</pre></td><td align="center" style="background-color:rgb(255,63,63);color:#ffffff;width:50px">&nbsp;</td></tr>
	<tr><td class="code"><pre>map[6]</pre></td><td align="center" style="background-color:rgb(255,127,127);color:#ffffff;width:50px">&nbsp;</td></tr>
	<tr><td class="code"><pre>map[7]</pre></td><td align="center" style="background-color:rgb(255,191,191);color:#ffffff;width:50px">&nbsp;</td></tr>
	<tr><td class="code"><pre>map[8]</pre></td><td align="center" style="background-color:rgb(255,255,255);color:#ffffff;width:50px">&nbsp;</td><td>white</td></tr>
</table>


